home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / jwlthief.zip / GUARD.CLS < prev    next >
Text File  |  1988-09-09  |  3KB  |  115 lines

  1. /* The ever menecing protector of the jewels. */
  2. /* A bitmap is used to quickly draw and erase */
  3. /* the guard.  This requires MS-Windows calls */
  4. /* but is faster than using a polygon.        */!!
  5.  
  6. inherit(Object, #Guard, #(bitmap /* guard's graphic def'n */
  7. dir    /* direction of movement */
  8. pen    /* Object to draw/erase  */
  9. speed  /* how quick he goes!    */
  10. x      /* x position            */
  11. y      /* y position            */), 2, nil)!!
  12.  
  13. now(GuardClass)!!
  14.  
  15. /* Create and initialize a new guard. */
  16. /**************************************/
  17. Def  new(self | guard, dirArray)
  18. { guard := new(self:Behavior);  /* Get something to work with. */
  19.   guard.x := random(222) + 5;   /* Get an initial position. */
  20.   guard.y := random(186) + 5;
  21.   guard.speed := random(4) + 4; /* Find out how fast we are */
  22.   guard.dir := new(Array, 2);   /* Find out which way we're going */
  23.   dirArray := #("N", "S", "W", "E");
  24.   guard.dir[0] := dirArray[random(1)];
  25.   guard.dir[1] := dirArray[random(1)+2];
  26.                                 /* Find out what we look like */
  27.   guard.bitmap := Call LoadBitmap(HInstance, asciiz("funman"));
  28.   setGraphics(guard);           /* Find out how to draw/erase ourself */
  29.   ^guard
  30. }!!
  31.  
  32. now(Guard)!!
  33.  
  34. /* Return size in pixels. */
  35. /**************************/
  36. Def  size(self)
  37. { ^10
  38. }!!
  39.  
  40. /* Set up the devices to draw & erase. */
  41. /***************************************/
  42. Def  setGraphics(self)
  43. { pen := Call GetStockObject(BLACK_BRUSH);
  44. }!!
  45.  
  46. /* Return the mov't factor. */
  47. /****************************/
  48. Def  speed(self)
  49. { ^speed
  50. }!!
  51.  
  52. /* Change to the specified direction. */
  53. /**************************************/
  54. Def  setDir(self, newDir)
  55. {  select
  56.     case newDir = "N" cor newDir = "S"
  57.     is dir[0] := newDir;
  58.     endCase
  59.     case newDir = "W" cor newDir = "E"
  60.     is dir[1] := newDir;
  61.     endCase
  62.   endSelect;
  63. }!!
  64.  
  65. /* Return the guard's y position */
  66. /*********************************/
  67. Def  y(self)
  68. { ^y
  69. }!!
  70.  
  71. /* Return the guard's x position */
  72. /*********************************/
  73. Def  x(self)
  74. { ^x
  75. }!!
  76.  
  77. /* Display the guard.           */
  78. /* Lots of MS-Windows but fast. */
  79. /********************************/
  80. Def  draw(self, hDC | hMemDC)
  81. { Call SelectObject(hDC, pen);             /* Get pen for the window.  */
  82.   hMemDC := Call CreateCompatibleDC(hDC);  /* Create a compatable hDC. */
  83.   Call SelectObject(hMemDC, bitmap);       /* Get bitmap image.        */
  84.   Call SelectObject(hMemDC, pen);          /* Get pen for the bitmap.  */
  85.   Call BitBlt(hDC, x-10, y-10, 20, 20,     /* Draw (or erase) bitmap   */
  86.       hMemDC, 0, 0, 0x004916C5);           /*   by inverting pixels.   */
  87.   Call DeleteDC(hMemDC);                   /* Clean up.                */
  88.  
  89. }!!
  90.  
  91. /* Move the guard based on */
  92. /* dir and speed.          */
  93. /***************************/
  94. Def  move(self, hDC)
  95. { draw(self, hDC);     /* Erase the guard. */
  96.   select               /* Set new position. */
  97.     case dir[0] = "N"
  98.     is y := y - speed;
  99.     endCase
  100.     case dir[0] = "S"
  101.     is y := y + speed;
  102.     endCase
  103.   endSelect;
  104.   select
  105.     case dir[1] = "W"
  106.     is x := x - speed;
  107.     endCase
  108.     case dir[1] = "E"
  109.     is x := x + speed;
  110.     endCase
  111.   endSelect;
  112.   draw(self, hDC);    /* Draw the guard. */
  113. }!!
  114.  
  115.